<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\config\Status;
use app\models\ChildFlow;
use app\modules\v1\controllers\ChildFlowController;
use app\models\FlowType;
use app\models\Project;
use app\models\User;
use app\modules\v1\controllers\parent\ParentController;
use Yii;
use yii\data\ActiveDataProvider;

class FlowTypeController extends ParentController
{

    public function actionIndex() {
        $params = Yii::$app->request->getQueryParams();
        $query = FlowType::find();
        $query->andWhere(['valid'=>1]);
        $pageSize = 4;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);
    }
    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionCreate(){
        $data = Yii::$app->request->getBodyParams();
        $model = new FlowType();
        $model->load($data, '');

        if ($model->save()) {
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $model,
            ];
        }

        return [
            'bCode' => Status::SUCCESS,
            'bData' => $model->errors,
        ];
    }

    // 获取已有阶段数据
    public function actionViews() {
        $params = Yii::$app->request->getQueryParams();
        $query = FlowType::findOne(['project_id' => $params['project_id'], 'valid' =>1]);
        return $query;
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdate() {
        $data = Yii::$app->request->getBodyParams();
        $query = FlowType::findOne(['project_id' => $data['project_id'], 'valid' => 1]);
        $query->load($data, '');
        if ($query->save()) {
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $query,
            ];
        }
        return [
            'bCode' => Status::SUCCESS,
            'bData' => $query->errors,
        ];
    }
}